package com.leo.manage_system.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leo.manage_system.entity.User;
import com.leo.manage_system.entity.UserWechat;
import com.leo.manage_system.entity.vo.UserVo;
import com.leo.manage_system.service.UserService;
import com.leo.manage_system.service.UserWechatService;
import com.leo.manage_system.utils.JwtUtils;
import com.leo.manage_system.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-12-25
 */
@Api(tags = "其他平台的用户管理")
@CrossOrigin
@RestController
@RequestMapping("/service_manage/user")
public class UserController {
    JwtUtils jwtUtils = new JwtUtils();
    final UserService userService ;
    final UserWechatService userWechatService ;
    @Autowired
    public UserController(UserService userService,UserWechatService userWechatService) {
        this.userService = userService;
        this.userWechatService = userWechatService;
    }

    @ApiOperation(value = "查询双平台用户总数量")
    @GetMapping("getTotal")
    public R getTotal(@ApiParam(name = "token", value = "token", required = true)
                         @RequestParam(value="token") String token) {
        if(jwtUtils.verify(token)==2){
            return R.error().message("token已过期");
        }
        else if(jwtUtils.verify(token)==0){
            return R.error().message("token认证失败");
        }
        else {
            Page<User> pageParam1 = new Page<>(1, 1);
            Page<UserWechat> pageParam2 = new Page<>(1, 1);
            Object user_total=userService.getUserPageQuery(pageParam1,null).get("total");
            Object userWechat_total=userWechatService.getUserWechatPageQuery(pageParam2,null).get("total");


            int sum_total = Integer.parseInt(user_total.toString())+Integer.parseInt(userWechat_total.toString());
            return R.ok().data("user_total",user_total).data("userWechat_total", userWechat_total).data("sum_total", sum_total);
        }

    }
    @ApiOperation(value = "查询双平台用户当天注册量")
    @GetMapping("getNewUser")
    public R getNewUser(@ApiParam(name = "token", value = "token", required = true)
                         @RequestParam(value="token") String token) {
        if(jwtUtils.verify(token)==2){
            return R.error().message("token已过期");
        }
        else if(jwtUtils.verify(token)==0){
            return R.error().message("token认证失败");
        }
        else {
            int UserTodayRegistrations=userService.getUserTodayRegistrations();
            int UserWechatTodayRegistrations=userWechatService.getUserWechatTodayRegistrations();
            return R.ok().data("user",UserTodayRegistrations).data("userWechat", UserWechatTodayRegistrations).data("sum", UserTodayRegistrations+UserWechatTodayRegistrations);
        }

    }


    @ApiOperation(value = "分页用户列表")
    @GetMapping("UserPageQuery/{page}/{limit}")
    public R getUserPageQuery(
            @ApiParam(name = "token", value = "token", required = true)
            @RequestParam(value="token") String token,
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,
            @ApiParam(name = "resQuery", value = "查询", required = false)
            @RequestBody(required = false) UserVo userVo
    ){
        if(jwtUtils.verify(token)==2){
            return R.error().message("token已过期");
        }
        else if(jwtUtils.verify(token)==0){
            return R.error().message("token认证失败");
        }
        else {
            Page<User> pageParam = new Page<>(page, limit);
            return R.ok().data(userService.getUserPageQuery(pageParam,userVo));
        }

    }

    @ApiOperation(value = "根据openID删除用户")
    @DeleteMapping("deleteUser")
    public R deleteAdmin(
            @ApiParam(name = "token", value = "token", required = true)
            @RequestParam(value="token") String token,
            @ApiParam(name = "openId", value = "openId", required = true)
            @RequestParam String openId){

        if(jwtUtils.verify(token)==2){
            return R.error().message("token已过期");
        }
        else if(jwtUtils.verify(token)==0){
            return R.error().message("token认证失败");
        }
        else {
            boolean code = userService.removeById(openId);
            if(!code) {
                return R.error().message("删除失败");
            }
            return R.ok().message("删除成功");
        }
    }
    @ApiOperation("根据openID编辑用户")
    @PostMapping("/modifyUser")
    public R modifyRes(
            @ApiParam(name = "token", value = "token", required = true)
            @RequestParam(value="token") String token,
            @ApiParam("用户") @RequestBody User user) {
        if(jwtUtils.verify(token)==2){
            return R.error().message("token已过期");
        }
        else if(jwtUtils.verify(token)==0){
            return R.error().message("token认证失败");
        }
        else {
            boolean code = userService.updateById(user);
            if(!code) {
                return R.error().message("编辑失败");
            }
            return R.ok().message("编辑成功");
        }
    }
}

